package cn.edu.scau.cmi.liweihang.composite.safe;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputDialog;

public class Control {
    @FXML
	private TextField teamField;
    @FXML
	private TextField personField;
    @FXML
	private TextField searchField;
    @FXML
    private TextArea textarea;
    
    private  static StringBuffer output;
    
  
    private Team root=new Team();
    
    private Component temp;
    
    public void set() {
    	root.setName("各大组织");
//    	Team team1=new Team();
//		Team team11=new Team();
//		Team team12=new Team();
//		Team temp=new Team();
//		Person person11=new Person();
//		Person person12=new Person();
//		Person person121=new Person();
//		Person person122=new Person();
//		Person person123=new Person();
//		Person person124=new Person();
//		team1.setName("team1");
//		team11.setName("team11");
//		team12.setName("team12");
//		person11.setName("person11");
//		person12.setName("person12");
//		person121.setName("person121");
//		person122.setName("person122");
//		person123.setName("person123");
//		person124.setName("person124");
//		root.addChild(team1);
//		add(team1, person11);
//		add(team1, person12);
//		add(team1, team11);
//		add(team11,person121);
//		add(team11,person122);
//		add(team11,person123);
//		add(team1,team12);		
		searchAll(root, 0);
    }
    
    public void add() {
    	if(teamField.getText().length()!=0&&personField.getText().length()==0) {//插入的是team不是person
    		Team team=new Team();
    		team.setName(teamField.getText());
    		if(searchField.getText().length()!=0) {//指定了插入的组织或个人
    			temp=search(root, searchField.getText());
    			if(temp instanceof Team) {//如果指定插入的是一个组织，则直接插入变成该组织的child
    				add(((Team) temp),team);
    				textarea.setText("");
    				searchAll(root,0);
    			}else if(temp instanceof Person){//如果指定插入的是个人，则将他纳入组织，并成为它的父节点的孩子
    				Person person=new Person();
    				person=(Person) temp;
    				temp=person.getTeam();
    				((Team) temp).removeChild(person);
    				add(temp,team);
    				add(team,person);
    				textarea.setText("");
    				searchAll(root, 0);
    			}
    		}else {//若没有指定插入的组织或个人，则默认插入到root
    			add(root,team);
    			textarea.setText("");
    			searchAll(root,0);
    		}
    	}else {//插入的是person不是team
    		Person person=new Person();
    		person.setName(personField.getText());
    		if(searchField.getText().length()!=0) {//指定了插入组织或个人
    			temp=search(root,searchField.getText());
    			if(temp instanceof Team) {//如果指定插入的是一个组织，作为该组织的孩子直接插入
    				add(temp,person);
    				textarea.setText("");
    				searchAll(root,0);
    			}else if(temp instanceof Person) {//如果指定插入的是个人，则创建一个团队，将原个人和新插入个人组合为一个团队
    				Team nt=new Team();
    				Person ptemp=new Person();
    				ptemp=(Person) temp;
    				temp=temp.getTeam();
    				((Team) temp).removeChild(search(root,searchField.getText()));
    				TextInputDialog ti=new TextInputDialog("请输入团队名称");
    			    Optional<String> opt=ti.showAndWait();
    				ti.setTitle("创建团队");
    				ti.setHeaderText("");
    				nt.setName(opt.get());
    				add(temp,nt);
    				add(nt,person);
    				add(nt,ptemp);
    				textarea.setText("");
    				searchAll(root,0);
    			}
    		}else {//若没有指定插入的组织或个人，则默认插入到root
    			add(root,person);
    			textarea.setText("");
    			searchAll(root,0);
    		}
    	}
    	
    }
    
    
    
    
    public static void add(Component father,Component son) {
		((Team) father).addChild(son);
		son.setTeam(father);
	}
	
	

	public static Component search(Component team,String name) {
		if(team instanceof Team) {
			if(team.getName().equals(name)) {
				return team;
			}	
			Set<Component> children=new HashSet();
	        children=((Team)team).getChildren();
	        Component result=null;
            for(Component temp:children) {
            	result=search(temp,name);
            	if(result!=null) {
            		return result;
            	};
	        }
		}
		else if(team instanceof Person){
			if(team.getName().equals(name)) {
				return team;
			}
		}
		return null;
	}
	
	public  void searchAll(Component team,int count) {
		count++;
		for(int i=0;i<count;i++) {
			textarea.appendText("--");
		}
		if(team instanceof Team) {
			Set<Component> children=new HashSet();
	        children=((Team)team).getChildren();
	        textarea.appendText("(team)"+((Team) team).getName()+"\n");
            for(Component temp:children) {
            	searchAll(temp,count);
	        }
		}
		else if(team instanceof Person){
			textarea.appendText("(person)"+((Person) team).getName()+"\n");
		}
		
	}
}
